<?php
/**
 * 报修
 */
namespace App\Controller\Member;

use Common\Controller\App\CommonController;
use Common\Model\HousingModel;
use Common\Model\HousingSkuModel;
use Common\Model\AdminModel;
use Common\Model\OrderModel;
use Common\Model\HousekeeperCommentModel;
use Common\Model\RepairModel;

class RepairController extends CommonController{

    /**
     * 获取参数
     */
    public function getInfo(){
        $member_id = session('member_id');
        $model = new OrderModel();
        $time = date('Y-m-d H:i:s',time());
        $info = $model->where("member_id=$member_id and end_time>'$time'")->field("order_id,room_number,housing_id,start_time")->find();
        if($info){
            $model = new HousingModel();
            $info1 = $model->where("id={$info['housing_id']}")->field("housekeeper_id,pic,money")->find();
            $adminmodel = new AdminModel();
            $admininfo = $adminmodel->where("id={$info1['housekeeper_id']}")->field('username,intro,logo,mobile,id')->find();
            $info['housekeeper_username'] = $admininfo['username'];
            $info['housekeeper_intro'] = $admininfo['intro'];
            $info['housekeeper_logo'] = $admininfo['logo'];
            $info['housekeeper_mobile'] = $admininfo['mobile'];
            $infos = array_merge($info,$info1);
            $skumodel = new HousingSkuModel();
            $list = $skumodel->where("pid={$info['housing_id']}")->field("id,number")->select();
            $infos['list'] = $list;
            $hmodel = new HousekeeperCommentModel();
            $star = $hmodel->where("housekeeper_id={$admininfo['id']}")->order('create_time desc')->getField('star');
            $infos['housekeeper_star'] = $star ? $star : 5;
        }else{
            $infos = [];
        }

        $this->ajaxReturn(['code'=>200,'info'=>$infos]);
    }

    /**
     * 报修添加
     */
    public function add(){
        $member_id = session('member_id');
        $housing_id = I('param.housing_id'); //房源id
        $name = I('param.name');  //姓名
        $mobile = I('param.mobile');  //手机号
        $add_time = I('param.add_time');  //入住时间
        $detailed = I('param.detailed');  //描述
        if(empty($housing_id)){
            $this->ajaxReturn(['code'=>300,'msg'=>'房源编号错误']);
        }
        if(empty($name)){
            $this->ajaxReturn(['code'=>300,'msg'=>'请填写姓名']);
        }
        if(empty($mobile)){
            $this->ajaxReturn(['code'=>300,'msg'=>'请填写手机号']);
        }
        $data['housing_id'] = $housing_id;
        $data['name'] = $name;
        $data['member_id'] = $member_id;
        $data['mobile'] = $mobile;
        $data['add_time'] = $add_time;
        $data['detailed'] = $detailed;
        $data['create_time'] = date('Y-m-d H:i:s',time());
        $model = new RepairModel();
        $res = $model->add($data);
        if(empty($res)){
            $this->ajaxReturn(['code'=>300,'msg'=>'提交失败']);
        }else{
            $this->ajaxReturn(['code'=>200,'msg'=>'提交成功']);
        }
    }
}